package org.hgvc.webapp.ets.spring.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hgvc.webapp.ets.bo.ActionBulletin;
import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.bo.PartnerProduct;
import org.hgvc.webapp.ets.bo.RCIAssignment;
import org.hgvc.webapp.ets.bo.RCIAssignmentDetail;
import org.hgvc.webapp.ets.bo.RCIBooking;
import org.hgvc.webapp.ets.bo.RCIBookingEntry;
import org.hgvc.webapp.ets.bo.RCIBulkBank;
import org.hgvc.webapp.ets.bo.RCIIncomingMember;
import org.hgvc.webapp.ets.bo.RCIMember;
import org.hgvc.webapp.ets.bo.RCISplitWeekConfig;
import org.hgvc.webapp.ets.constant.ActionBulletinConstant;
import org.hgvc.webapp.ets.constant.BookingConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.SystemConstant;
import org.hgvc.webapp.ets.jsf.beans.admin.AdminMemberBookingBean;
import org.hgvc.webapp.ets.jsf.beans.admin.AdminRCIBean;
import org.hgvc.webapp.ets.spring.dao.RCIHbmDao;
import org.jocsb.base.bo.User;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.DateUtil;
import org.jocsb.base.util.SpringUtils;
import org.jocsb.base.util.SystemUtil;


public class AdminRCIServiceImpl implements AdminRCIService{
	private RCIHbmDao dao;
	private AdminService adminService;
	private MemberService memberService;
	private SystemService systemService;
	
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}
	public void setDao(RCIHbmDao dao) {
		this.dao = dao;
	}
	
	public RCIBulkBank getRCIBulkBank(String id)throws BaseException{
		RCIBulkBank rciBulkBank = null;
		try{
			rciBulkBank = (RCIBulkBank) dao.getObject(RCIBulkBank.class, id);
		}catch(Exception ex){
			throw new BaseException(SpringUtils.getMessage("error.get.object", new Object[]{RCIBulkBank.class.getSimpleName()}));
		}
		return rciBulkBank;
	}
	
	public RCIBookingEntry getRCIBookingEntry(String id)throws BaseException{
		RCIBookingEntry rciBookingEntry=null;
		try{
			rciBookingEntry = (RCIBookingEntry) dao.getObject(RCIBookingEntry.class, id);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.get.object", new Object[]{RCIBookingEntry.class.getSimpleName()}));
		}
		return rciBookingEntry;
	}
	
	public List<RCIBookingEntry> getRCIBookingEntryList()throws BaseException{
		List<RCIBookingEntry> list = new ArrayList<RCIBookingEntry>();
		try{
			list=dao.getRCIBookingEntryList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIBookingEntry.class.getSimpleName()}));
		}
		return list;
	}
	public void saveRCIBookingEntry(RCIBookingEntry rciBookingEntry)throws BaseException{
		
		try{
			RCIBulkBank rciBulkBank = (RCIBulkBank) dao.getObject(RCIBulkBank.class, rciBookingEntry.getRciBulkBank().getRciBulkBankId());
			if (rciBulkBank==null)
				throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{RCIBulkBank.class.getSimpleName()}));
			
			if (rciBookingEntry.getRciType().equalsIgnoreCase(BookingConstant.RCI_TYPE_OUTGOING)){//validate outgoing member
				/*RCIMember rciMember = (RCIMember) dao.getObject(RCIMember.class, rciBookingEntry.getRciMemberRefId());
				
				if (rciMember==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{RCIMember.class.getSimpleName()}));
				
				Agreement agreement = rciMember.getAgreement();
				if (agreement==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{Agreement.class.getSimpleName()}));
				
				Membership membership = agreement.getMembership();
				if (membership==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{Membership.class.getSimpleName()}));
				
				Integer requireRCIPoint =  membership.getRciPointDec();
				
				memberService.validateMemberPoint(agreement.getAgreementId(), requireRCIPoint);
				
				rciBookingEntry.setOutgoingPointReserved(requireRCIPoint);
				
				memberService.reserveMemberPoint(agreement.getAgreementId(), requireRCIPoint);
				
				rciBookingEntry.setRciMemberFullName(rciMember.getRciMemberName());
				
				Date checkInDate = DateUtil.getDateByWeek(rciBulkBank.getYear(), rciBulkBank.getWeek(), Calendar.SUNDAY);
				Date checkOutDate = DateUtil.getDateByWeek(rciBulkBank.getYear(), rciBulkBank.getWeek(), Calendar.SATURDAY);
				
				//Date outgoingCheckOutDate = DateUtil.getNextDate(rciBookingEntry.getRciCheckInDate(), 7);
				rciBookingEntry.setRciCheckInDate(checkInDate);
				rciBookingEntry.setRciCheckOutDate(checkOutDate);*/
			}else{
				//RCIIncomingMember rciIncomingMember = (RCIIncomingMember) dao.getObject(RCIIncomingMember.class, rciBookingEntry.getRciMemberRefId());
				//if (rciIncomingMember==null)
				//	throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{RCIIncomingMember.class.getSimpleName()}));
				rciBookingEntry.setOutgoingPointReserved(new Integer(0));
				//rciBookingEntry.setRciMemberFullName(rciIncomingMember.getFullName());
				
				RCISplitWeekConfig splitWeekConfig = (RCISplitWeekConfig) dao.getObject(RCISplitWeekConfig.class, rciBookingEntry.getRciSplitWeekConfig().getRciSplitWeekConfigId());
				if (splitWeekConfig==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{RCISplitWeekConfig.class.getSimpleName()}));
				
				Date checkInDate = DateUtil.getDateByWeek(rciBulkBank.getYear(), rciBulkBank.getWeek(), splitWeekConfig.getCheckInDay());
				rciBookingEntry.setRciCheckInDate(checkInDate);
				Date checkOutDate = DateUtil.getDateByWeek(rciBulkBank.getYear(), rciBulkBank.getWeek(), splitWeekConfig.getCheckOutDay());
				rciBookingEntry.setRciCheckOutDate(checkOutDate);
			}
			
			rciBookingEntry.setRciBulkBank(rciBulkBank);
			rciBookingEntry.setRciWeek(rciBulkBank.getWeek());
			
			dao.save(rciBookingEntry);
			
			//update RCIBulkBank
			rciBulkBank.setAvailableFlag(new Integer(1));
			rciBulkBank.setReservedType(BookingConstant.RCI_TYPE_INCOMING);
			rciBulkBank.setRefId(rciBookingEntry.getRciBookingEntryId());
			dao.update(rciBulkBank);
			
			//Insert ActionBulletin
			ActionBulletin ab = new ActionBulletin();
			ab.setActionBean(AdminRCIBean.BEAN_NAME);
			ab.setActionMethod(AdminRCIBean.AB_ACTION_INVOKE);
			ab.setActionCode(SpringUtils.getMessage("ab.new.rci.booking.pending.approval", new Object[]{}));
			ab.setActionParam1(rciBookingEntry.getRciBookingEntryId());
			ab.setCreatedBy(SystemConstant.SYSTEM_USER);
			ab.setCreatedDate(new Date());
			ab.setIsDeleted(CommonConstant.NOT_DELETED);
			ab.setType(ActionBulletinConstant.AB_ACTION_TYPE);
			ab.setActionNo(SystemUtil.getUniqueCode());
			systemService.createActionBulletinByUser(ab, rciBulkBank.getPartnerProduct().getPartner().getRefUser());
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{RCIBookingEntry.class.getSimpleName()}));
		}
	}
	public void updateRCIBookingEntry(RCIBookingEntry rciBookingEntry)throws BaseException{
		
		try{
			
			/*if (rciBookingEntry.getRciType().equalsIgnoreCase(BookingConstant.RCI_TYPE_OUTGOING)){//validate outgoing member
				RCIMember rciMember = (RCIMember) dao.getObject(RCIMember.class, rciBookingEntry.getRciMemberRefId());
				
				if (rciMember==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{RCIMember.class.getSimpleName()}));
				
				Agreement agreement = rciMember.getAgreement();
				if (agreement==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{Agreement.class.getSimpleName()}));
				
				if (rciBookingEntry.getStatus().equalsIgnoreCase(BookingConstant.STATUS_CANCEL)){
					memberService.cancelMemberPoint(agreement.getAgreementId(), rciBookingEntry.getOutgoingPointReserved());
				}else if (rciBookingEntry.getStatus().equalsIgnoreCase(BookingConstant.STATUS_CONFIRM)){
					memberService.deductMemberPoint(agreement.getAgreementId(), rciBookingEntry.getOutgoingPointReserved());
				}
			}*/
			
			dao.update(rciBookingEntry);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.update.object", new Object[]{RCIBookingEntry.class.getSimpleName()}));
		}
	}
	
	//RCI Incoming Only
	public void approveRCIBookingEntry(RCIBookingEntry rciBookingEntry,String actionNo,User logonUser)throws BaseException{
		try{
			rciBookingEntry.setAssignBy(logonUser);
			rciBookingEntry.setAssignConfirmationNoFlag(CommonConstant.STATUS_YES);
			rciBookingEntry.setAssignDate(new Date());
			rciBookingEntry.setStatus(BookingConstant.STATUS_CONFIRM);
			dao.update(rciBookingEntry);
			
		/*	if (rciBookingEntry.getRciType().equalsIgnoreCase(BookingConstant.RCI_TYPE_OUTGOING)){
				RCIMember rciMember = (RCIMember) dao.getObject(RCIMember.class, rciBookingEntry.getRciMemberRefId());
				
				if (rciMember==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{RCIMember.class.getSimpleName()}));
				
				Agreement agreement = rciMember.getAgreement();
				if (agreement==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{Agreement.class.getSimpleName()}));
				
				memberService.deductMemberPoint(agreement.getAgreementId(), rciBookingEntry.getOutgoingPointReserved());
			}*/
			systemService.deleteActionBulletinByActionNo(actionNo);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.invoke.method", new Object[]{"approveRCIBookingEntry()"}));
		}
	}
	
	public void rejectRCIBookingEntry(RCIBookingEntry rciBookingEntry,String actionNo,User logonUser)throws BaseException{
		try{
			rciBookingEntry.setStatus(BookingConstant.STATUS_CANCEL);
			dao.update(rciBookingEntry);
			
		/*	if (rciBookingEntry.getRciType().equalsIgnoreCase(BookingConstant.RCI_TYPE_OUTGOING)){
				RCIMember rciMember = (RCIMember) dao.getObject(RCIMember.class, rciBookingEntry.getRciMemberRefId());
				
				if (rciMember==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{RCIMember.class.getSimpleName()}));
				
				Agreement agreement = rciMember.getAgreement();
				if (agreement==null)
					throw new BaseException(SpringUtils.getMessage("error.object.not.found", new Object[]{Agreement.class.getSimpleName()}));
				
				memberService.cancelMemberPoint(agreement.getAgreementId(), rciBookingEntry.getOutgoingPointReserved());
			}*/
			
			RCIBulkBank rciBulkBank = (RCIBulkBank) dao.getObject(RCIBulkBank.class, rciBookingEntry.getRciBulkBank().getRciBulkBankId());
			rciBulkBank.setAvailableFlag(new Integer(0));
			rciBulkBank.setRefId(null);
			rciBulkBank.setReservedType(null);
			dao.update(rciBulkBank);
			
			systemService.deleteActionBulletinByActionNo(actionNo);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.invoke.method", new Object[]{"rejectRCIBookingEntry()"}));
		}
	}
	
	public void deleteRCIBookingEntry(List<RCIBookingEntry> deleteList,User logonUser)throws BaseException{
		try{
			for(RCIBookingEntry r : deleteList){
				r.setModifiedBy(logonUser);
				r.setModifiedDate(new Date());
				r.setIsDeleted(CommonConstant.IS_DELETED);
				
				RCIBulkBank rciBulkBank = (RCIBulkBank) dao.getObject(RCIBulkBank.class, r.getRciBulkBank().getRciBulkBankId());
				rciBulkBank.setAvailableFlag(new Integer(0));
				rciBulkBank.setRefId(null);
				rciBulkBank.setReservedType(null);
				dao.update(rciBulkBank);
				
				dao.update(r);
			}
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{RCIBookingEntry.class.getSimpleName()}));
		}
	}
	
	public List<RCISplitWeekConfig> getRCISplitWeekConfigList()throws BaseException{
		List<RCISplitWeekConfig> list = new ArrayList<RCISplitWeekConfig>();
		try{
			list=dao.getRCISplitWeekConfigList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCISplitWeekConfig.class.getSimpleName()}));
		}
		return list;
	}
	
	public void saveRCISplitWeekConfig(RCISplitWeekConfig rciSplitWeekConfig)throws BaseException{
		
		try{
			dao.save(rciSplitWeekConfig);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{RCISplitWeekConfig.class.getSimpleName()}));
		}
	}
	public void updateRCISplitWeekConfig(RCISplitWeekConfig rciSplitWeekConfig)throws BaseException{
		
		try{
			dao.update(rciSplitWeekConfig);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.update.object", new Object[]{RCISplitWeekConfig.class.getSimpleName()}));
		}
	}
	public void deleteRCISplitWeekConfig(List<RCISplitWeekConfig> deleteList,User logonUser)throws BaseException{
		try{
			for(RCISplitWeekConfig r : deleteList){
				r.setModifiedBy(logonUser);
				r.setModifiedDate(new Date());
				r.setIsDeleted(CommonConstant.IS_DELETED);
				dao.update(r);
			}
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{RCISplitWeekConfig.class.getSimpleName()}));
		}
	}
	
	public void saveRCIIncomingMember(RCIIncomingMember rciIncomingMember)throws BaseException{
	
		try{
			dao.save(rciIncomingMember);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{RCIIncomingMember.class.getSimpleName()}));
		}
	}
	public void updateRCIIncomingMember(RCIIncomingMember rciIncomingMember)throws BaseException{
		try{
			dao.update(rciIncomingMember);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.update.object", new Object[]{RCIIncomingMember.class.getSimpleName()}));
		}
	}
	public void deleteRCIIncomingMember(List<RCIIncomingMember> deleteList,User logonUser)throws BaseException{
		try{
			for(RCIIncomingMember rciIncomingMember : deleteList){
				rciIncomingMember.setModifiedBy(logonUser);
				rciIncomingMember.setModifiedDate(new Date());
				rciIncomingMember.setIsDeleted(CommonConstant.IS_DELETED);
				dao.update(rciIncomingMember);
			}
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{RCIIncomingMember.class.getSimpleName()}));
		}
	}
	public List<RCIIncomingMember> getRCIIncomingMemberList()throws BaseException{
		List<RCIIncomingMember> list = new ArrayList<RCIIncomingMember>();
		try{
			list=dao.getRCIIncomingMemberList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIIncomingMember.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<RCIBulkBank> getRCIBulkBankOutgoingList()throws BaseException{
		List<RCIBulkBank> list=new ArrayList<RCIBulkBank>();
		try{
			List<RCIBulkBank> currentlist=dao.getRCIBulkBankOutgoingList();
			for (RCIBulkBank r : currentlist){
				RCIMember rciMember = (RCIMember) dao.getObject(RCIMember.class, r.getRefId());
				r.setRciMember(rciMember);
				list.add(r);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIBulkBank.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<RCIBulkBank> getRCIBulkBankAvList()throws BaseException{
		List<RCIBulkBank> list=new ArrayList<RCIBulkBank>();
		try{
			list=dao.getRCIBulkBankAvList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIBulkBank.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<RCIBulkBank> getRCIBulkBankList()throws BaseException{
		List<RCIBulkBank> list=new ArrayList<RCIBulkBank>();
		try{
			list=dao.getRCIBulkBankList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIBulkBank.class.getSimpleName()}));
		}
		return list;
	}
	public List<RCIBulkBank> getRCILBulkBankList(String searchField,String searchKeyword)throws BaseException{
		List<RCIBulkBank> list=new ArrayList<RCIBulkBank>();
		try{
			list=dao.getRCIBulkBankList(searchField, searchKeyword);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIBulkBank.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<RCIMember> getRCIMemberList()throws BaseException{
		List<RCIMember> list=new ArrayList<RCIMember>();
		try{
			list=dao.getRCIMemberList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIMember.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<RCIAssignment> getRCIAssignmentList()throws BaseException{
		List<RCIAssignment> list=new ArrayList<RCIAssignment>();
		try{
			list=dao.getRCIAssignmentList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIAssignment.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<RCIAssignment> getRCIAssignmentList(RCIBulkBank rciBulkBank)throws BaseException{
		List<RCIAssignment> list=new ArrayList<RCIAssignment>();
		try{
			list=dao.getRCIAssignmentList(rciBulkBank);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIAssignment.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<RCIBooking> getRCIBookingList(RCIBulkBank rciBulkBank)throws BaseException{
		List<RCIBooking> list=new ArrayList<RCIBooking>();
		try{
			list=dao.getRCIBookingList(rciBulkBank);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIBooking.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<RCIAssignmentDetail> getRCIAssignmentDetailList(RCIAssignment rciAssignment)throws BaseException{
		List<RCIAssignmentDetail> list=new ArrayList<RCIAssignmentDetail>();
		try{
			list=dao.getRCIAssignmentDetailList(rciAssignment);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{RCIAssignmentDetail.class.getSimpleName()}));
		}
		return list;
	}
	
	public void saveRCIBulkBank(RCIBulkBank rciBulkBank)throws BaseException{
		try{
			PartnerProduct partnerProduct = (PartnerProduct)dao.getObject(PartnerProduct.class, rciBulkBank.getPartnerProduct().getPartnerProductId());
			rciBulkBank.setPartnerProduct(partnerProduct);
			dao.save(rciBulkBank);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{RCIBulkBank.class.getSimpleName()}));
		}
	}
	
	public void updateRCIBulkBank(RCIBulkBank rciBulkBank)throws BaseException{
		try{
			PartnerProduct partnerProduct = (PartnerProduct)dao.getObject(PartnerProduct.class, rciBulkBank.getPartnerProduct().getPartnerProductId());
			rciBulkBank.setPartnerProduct(partnerProduct);
			dao.update(rciBulkBank);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.update.object", new Object[]{RCIBulkBank.class.getSimpleName()}));
		}
	}
	
	public void deleteRCIBulkBank(List<RCIBulkBank> deleteList)throws BaseException{
		try{
			for(RCIBulkBank rciBulkBank : deleteList){
				List<RCIAssignmentDetail> list=dao.getRCIAssignmentDetailList(rciBulkBank);
				if (list.size()==0){
					rciBulkBank.setModifiedDate(new Date());
					rciBulkBank.setIsDeleted(CommonConstant.IS_DELETED);
					dao.update(rciBulkBank);
				}else{
					throw new BaseException(SpringUtils.getMessage("error.rcibulkbank.assigned", 
						new Object[]{rciBulkBank.getYear(),rciBulkBank.getPartnerProduct().getPartner().getCode(),
							rciBulkBank.getPartnerProduct().getCode(),rciBulkBank.getRoom1No()}));
				}
			}
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{RCIBulkBank.class.getSimpleName()}));
		}
	}
	
	public void saveRCIMember(RCIMember rciMember)throws BaseException{
		try{
			Member member = (Member)dao.getObject(Member.class, rciMember.getMember().getMemberId());
			Agreement agreement = (Agreement)dao.getObject(Agreement.class, rciMember.getAgreement().getAgreementId());
			rciMember.setMember(member);
			rciMember.setAgreement(agreement);
			dao.save(rciMember);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{RCIMember.class.getSimpleName()}));
		}
	}
	public void updateRCIMember(RCIMember rciMember)throws BaseException{
		try{
			Member member = (Member)dao.getObject(Member.class, rciMember.getMember().getMemberId());
			Agreement agreement = (Agreement)dao.getObject(Agreement.class, rciMember.getAgreement().getAgreementId());
			rciMember.setMember(member);
			rciMember.setAgreement(agreement);
			dao.update(rciMember);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.update.object", new Object[]{RCIMember.class.getSimpleName()}));
		}
	}
	public void deleteRCIMember(List<RCIMember> deleteList)throws BaseException{
		try{
			for (RCIMember rciMember : deleteList){
				List<RCIAssignment> list=dao.getRCIAssignmentList(rciMember);
				if (list.size()==0){
					rciMember.setIsDeleted(CommonConstant.IS_DELETED);
					rciMember.setModifiedDate(new Date());
					dao.update(rciMember);
				}else{
					throw new BaseException(SpringUtils.getMessage("error.rcimember.assigned", new Object[]{rciMember.getRciNo()}));
				}
			}
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{RCIMember.class.getSimpleName()}));
		}
	}
	
	public RCIAssignment saveRCIAssignment(RCIAssignment rciAssignment)throws BaseException{
		try{
			RCIMember rciMember=(RCIMember)dao.getObject(RCIMember.class, rciAssignment.getRciMember().getRciMemberId());
			rciAssignment.setRciMember(rciMember);
			String assignmentNo=adminService.getDocumentNo(CommonConstant.DOC_TYPE_RCI_ASSIGNMENT);
			rciAssignment.setRciAssignmentNo(assignmentNo);
			rciAssignment.setRciAssignmentDate(new Date());
			dao.save(rciAssignment);
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{RCIAssignment.class.getSimpleName()}));
		}
		return rciAssignment;
	}
	
	public void saveRCIAssignmentDetail(RCIAssignment rciAssignment,RCIAssignmentDetail rciAssignmentDetail)throws BaseException{
		try{
			RCIBulkBank rciBulkBank=(RCIBulkBank)dao.getObject(RCIBulkBank.class, rciAssignmentDetail.getRciBulkBank().getRciBulkBankId());
			rciAssignmentDetail.setRciBulkBank(rciBulkBank);
			rciAssignmentDetail.setRciAssignment(rciAssignment);
			dao.save(rciAssignmentDetail);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{RCIAssignmentDetail.class.getSimpleName()}));
		}
	}
	
	public void removeRCIAssignment(List<RCIAssignment> deleteList)throws BaseException{
		try{
			for(RCIAssignment rciAssignment : deleteList){
				List<RCIAssignmentDetail> list=dao.getRCIAssignmentDetailList(rciAssignment);
				if (list.size()>0){
					throw new BaseException(SpringUtils.getMessage("error.rciassignment.delete", new Object[]{rciAssignment.getRciAssignmentNo()}));
				}else{
					rciAssignment.setIsDeleted(CommonConstant.IS_DELETED);
					rciAssignment.setModifiedDate(new Date());
					dao.update(rciAssignment);
				}
			}
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{RCIAssignment.class.getSimpleName()}));
		}
	}
	
	public void removeRCIAssignmentDetail(List<RCIAssignmentDetail> deleteList)throws BaseException{
		try{
			for(RCIAssignmentDetail rciAssignmentDetail : deleteList){
				rciAssignmentDetail.setIsDeleted(CommonConstant.IS_DELETED);
				rciAssignmentDetail.setModifiedDate(new Date());
				dao.update(rciAssignmentDetail);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{RCIAssignmentDetail.class.getSimpleName()}));
		}
	}
}
